package com.group.service.impl;

import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.group.dao.TbContractMapper;
import com.group.dao.TbCustomerMapper;
import com.group.dao.TbDeviceAttrMapper;
import com.group.dao.TbDeviceMapper;
import com.group.pojo.Common;
import com.group.pojo.TbContract;
import com.group.pojo.TbContractExample;
import com.group.pojo.TbCustomer;
import com.group.pojo.TbDevice;
import com.group.pojo.TbDeviceExample;
import com.group.pojo.TbUser;
import com.group.pojo.TbContractExample.Criteria;
import com.group.service.CommonService;
import com.group.service.ContractService;
import com.group.service.CustomerService;
import com.group.service.DeviceService;
import com.group.util.CommonUtil;

@Service
public class ContractServiceImpl implements ContractService {
	@Autowired
	private TbContractMapper contractmapper;
	@Autowired
	private TbDeviceMapper devicemapper;
	@Autowired
	private TbCustomerMapper customermapper;
	@Autowired
	private CustomerService customerService;
	@Autowired
	private CommonService commonService;
	@Autowired
	private DeviceService deviceService;

	// 查询所有的合同
	@Override
	public List<TbContract> query() {
		// TODO Auto-generated method stub
		List<TbContract> list = contractmapper.selectByExample(null);
		for (int i = 0; i < list.size(); i++) {
			if ("0".equals(list.get(i).getContractStatus())) {
				list.remove(i);
				i--;
			}
		}
		return list;
	}

	// 通过设备id查询设备的名称
	@Override
	public String querydeviceName(String id) {
		TbDeviceExample example = new TbDeviceExample();
		example.createCriteria().andIdEqualTo(id).andDeviceStatusEqualTo("2");
		List<TbDevice> list = devicemapper.selectByExample(example);
		if (list.size() == 0) {
			return null;
		} else {
			return list.get(0).getDeviceName();
		}
	}

	// 添加新的合同
	@Override
	public Map<String, String> save(TbContract contract, HttpServletRequest request) {
		//设置创建人、创建时间
		TbUser loginUser = (TbUser) request.getSession().getAttribute("USER_SESSION");
		contract.setCreateUserId(loginUser.getId());
		contract.setCreateTime(new Date());

		//设置id
		contract.setId(commonService.getTbCurrentDateNextId(new Common("tb_contract", CommonUtil.getCurrentDate("yyyyMMdd"))));
		
		//设置状态
		contract.setContractStatus("1");
		
		Map<String, String> returnMap = CommonUtil.getReturnMap("success", "false");
		int insert = contractmapper.insertSelective(contract);

		if (insert == 1) {
			returnMap.put("success", "true");
		}
		return returnMap;
	}

	// 删除合同信息
	@Override
	public Map<String, String> deleteContractInfo(String[] contractIds) {
		TbContract contract = new TbContract();
		contract.setContractStatus("0");
		Map<String, String> map = CommonUtil.getReturnMap("success", "true");
		for (String contractid : contractIds) {
			contract.setId(contractid);
			int update = contractmapper.updateByPrimaryKeySelective(contract);
			if (update != 1) {
				map.put("success", "false");
				return map;
			}
		}
		return map;
	}

	// 通过合同id查询合同的信息
	@Override
	public TbContract getContractById(String id) {
		TbContractExample example = new TbContractExample();
		example.createCriteria().andIdEqualTo(id).andContractStatusEqualTo("1");
		List<TbContract> list = contractmapper.selectByExample(example);
		if (list.size() == 0) {
			return null;
		} else {
			return list.get(0);
		}
	}

	// 修改合同
	@Override
	public Map<String, String> updateContractSave(TbContract contract, String deviceName) {
		// 提交到数据库
		TbDevice device = new TbDevice();
		TbDeviceExample example = new TbDeviceExample();
		TbDeviceExample example1 = new TbDeviceExample();
		String deviceId = this.getContractById(contract.getId()).getDeviceId();
		if (!this.querydeviceName(deviceId).equals(deviceName)) {
			example.createCriteria().andDeviceStatusEqualTo("1").andDeviceNameEqualTo(deviceName)
					.andDeviceServiceNumberEqualTo(contract.getDeviceId());
			device.setDeviceStatus("2");
			devicemapper.updateByExampleSelective(device, example);// device为修改参数对象，exmaple为查询条件]
			contract.setDeviceId(deviceService.getDeviceIdByDeviceServiceNumber(contract.getDeviceId()));
			example1.createCriteria().andDeviceStatusEqualTo("2").andIdEqualTo(deviceId);
			device.setDeviceStatus("1");
			devicemapper.updateByExampleSelective(device, example1);// device为修改参数对象，exmaple为查询条件
		}
		contract.setCustomerId(customerService.getcustomerIdbyName(contract.getCustomerId()));
		contract.setDeviceId(deviceId);
		int insert = contractmapper.updateByPrimaryKeySelective(contract);
		Map<String, String> returnMap = CommonUtil.getReturnMap("success", "false");
		if (insert == 1) {
			returnMap.put("success", "true");
		}
		return returnMap;
	}
	//根据客户id和设备id查询合同
	@Override
	public List<TbContract> getcontractByDeviceIdAndCustomerId(String deviceId, String customerId) {
        TbContractExample example = new TbContractExample();
        example.createCriteria().andDeviceIdEqualTo(deviceId).andCustomerIdEqualTo(customerId).andContractStatusEqualTo("1");
        List<TbContract> list = contractmapper.selectByExample(example);
        if(list.size()!=0){
        	return list;
        }else{
        	return null;
        }
	}

	@Override
	public Map<String, String> update(TbContract contract) {
		Map<String, String> returnMap = CommonUtil.getReturnMap("success", "false");
		
		int update = contractmapper.updateByPrimaryKeySelective(contract);
		if (update == 1) {
			returnMap.put("success", "true");
		}
		return returnMap;
	}

	private void setCondition(Criteria criteria, TbContract contract) {
		String id = contract.getId();
		if(id != null && !"".equals(id)) {
			criteria.andIdEqualTo(id);
		}
		
		String customerId = contract.getCustomerId();
		if (customerId != null && !"".equals(customerId)) {
			criteria.andCustomerIdEqualTo(customerId);
		}
		
		String contractStatus = contract.getContractStatus();
		if (contractStatus != null && !"".equals(contractStatus)) {
			criteria.andContractStatusEqualTo(contractStatus);
		}
		
	}
	@Override
	public List<TbContract> query(TbContract contract) {
		TbContractExample example = new TbContractExample();
		
		setCondition(example.createCriteria(), contract);
		
		List<TbContract> list = contractmapper.selectByExample(example);
		return list;
	}

}
